#include "head.h"

#define FIFO_NAME "./fifo_test"

int main(int argc, char const *argv[])
{
    mkfifo( FIFO_NAME, 0777 );
    int FifoFd;
    FifoFd = open( FIFO_NAME, O_RDONLY );
    if ( -1 == FifoFd )
    {
        perror( "fail open fifo" );
        return -1;
    }    

    fd_set ReadFd;
    FD_ZERO( &ReadFd );
    FD_SET( 0, &ReadFd );
    FD_SET( FifoFd, &ReadFd );

    fd_set ReadFdTmp;

    while (1)
    {
        ReadFdTmp = ReadFd;

        int Ret;
        Ret = select( FifoFd + 1, &ReadFdTmp, NULL, NULL, NULL );
        if ( -1 == Ret )
        {
            perror( "fail select" );
            return -1;
        }

        if( FD_ISSET( 0, &ReadFdTmp ) )
        {
            char strBuffReadStdin[100] = {0};
            read( 0, strBuffReadStdin, sizeof(strBuffReadStdin) );
            printf( "Recvfrom stdin: %s\n", strBuffReadStdin );
        }

        if ( FD_ISSET( FifoFd, &ReadFdTmp ) )
        {
            char strBuffReadFifo[1024] = {0};
            read( FifoFd, strBuffReadFifo, sizeof(strBuffReadFifo) );
            printf( "Recvfrom fifo : %s\n", strBuffReadFifo );
        }
    }
    
    close( FifoFd );

    return 0;
}

/*
程序功能
涉及函数
知识要点
测试流程
*/